<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工艺名称" prop="craftName">
        <el-input
          v-model="queryParams.craftName"
          placeholder="请输入工艺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺编码" prop="craftCode">
        <el-input
          v-model="queryParams.craftCode"
          placeholder="请输入工艺编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:craft:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:craft:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:craft:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:craft:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="craftList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="工艺名称" align="center" prop="craftName" />
      <el-table-column label="工艺编码" align="center" prop="craftCode" />
      <!-- <el-table-column label="产品id" align="center" prop="productId" /> -->
      <el-table-column label="产品名称" align="center" prop="productName" />
      <!-- <el-table-column label="产品编码" align="center" prop="productCode" /> -->
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="创建人" align="center" prop="userId" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="note" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:craft:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:craft:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工艺管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称">
          <el-select v-model="form.productName" placeholder="请选择" @change="bomchange">
            <el-option v-for="item in productList" :value="item.productName">
          </el-option>
         </el-select>
          </el-form-item>
         <el-form-item label="产品编码:" prop="productCode">
          {{ this.form.productCode }}
         </el-form-item>
        <el-form-item label="工艺名称" prop="craftName">
          <el-input v-model="form.craftName" placeholder="请输入工艺名称" />
        </el-form-item>
        <el-form-item label="工艺编码" prop="craftCode">
          <el-input v-model="form.craftCode" placeholder="请输入工艺编码" />
        </el-form-item>
        <!-- <el-form-item label="产品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品id" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item> -->
        <!-- <el-form-item label="状态" prop="status">
          <el-input v-model="form.status" placeholder="请输入状态" />
        </el-form-item> -->
        <!-- <el-form-item label="创建人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入创建人" />
        </el-form-item> -->
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="工序:">
        </el-form-item>
        <!-- 工序子表 -->
        <el-table
        :data="form.processes"
        style="width: 100%"
        max-height="250">
        <el-table-column type="index" width="50" label="序号">
        </el-table-column>
        <el-table-column align="center" label="工序名称" width="160">
          <template slot-scope="scope">
            <el-select
            v-model="form.processes[scope.$index].processName"
              placeholder="请选择"
              style="width: 130px"
            >
              <el-option
                v-for="item in processList"
                :value="item.processName"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="station" label="工位" width="160">
          <template slot-scope="scope">
            <el-select
            v-model="form.processes[scope.$index].station"
              placeholder="请选择"
              style="width: 130px"
            >
              <el-option
                v-for="item in processList"
                :value="item.station"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column
         align="center"
          label="原材料"
          width="120">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handlelook(scope.$index,scope.row)"
              >查看</el-button
            >
          </template>
        </el-table-column>
        <el-table-column align="center" label="设备" width="160">
          <template slot-scope="scope">
            <el-select
            v-model="form.processes[scope.$index].equName"
              placeholder="请选择"
              style="width: 130px"
            >
              <el-option
                v-for="item in equipmentselectoption"
                :value="item.equName"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="deleteRow(scope.$index, form.processes)"
              type="text"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="primary" @click="processadd">添加</el-button>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看工序里物料的信息 -->
    <el-dialog title="BOM详情" :visible.sync="detailVisible" width="50%">
      <el-form  label-width="80px">
         <!-- 物料子表 -->
       <el-table
    :data="nowmtrlist"
    style="width: 100%"
    max-height="250">
    <el-table-column
      fixed
      type="index"
      label="序号"
      width="150">
    </el-table-column>
    <el-table-column align="center" label="物料名称" width="160">
      <template slot-scope="scope">
        <el-select
        v-model="nowmtrlist[scope.$index].materialName"
          placeholder="请选择"
          style="width: 130px"
        >
          <el-option
            v-for="item in materialselectoption"
            :value="item.materialName"
          >
          </el-option>
        </el-select>
      </template>
    </el-table-column>
    <el-table-column align="center" label="数量" width="220">
      <template slot-scope="scope">
        <el-input-number
          v-model="nowmtrlist[scope.$index].materialNum"
        ></el-input-number>
      </template>
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作"
      width="120">
      <template slot-scope="scope">
        <el-button
          @click.native.prevent="deletemat(scope.$index,nowmtrlist)"
          type="text"
          size="small">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-button type="primary" @click="mtradd">添加</el-button>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailVisible=false">确 定</el-button>
        <!-- <el-button @click="cancel">取 消</el-button> -->
      </div>
    </el-dialog>



  </div>
</template>

<script>
import { listCraft, getCraft, delCraft, addCraft, updateCraft,lookprocess,addCraftprocess } from "@/api/mes/craft";
import {listProduct,getProduct} from "@/api/mes/product";
import { listProcess,listequipment,addprocessmat} from "@/api/mes/process";
import { lookmaterial} from "@/api/mes/BOM";

export default {
  name: "Craft",
  data() {
    return {
      //当前查看的工艺物料列表
      nowmtrlist:[],
    //工序的值
    name:'',
    //产品名称的值
      value:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工艺管理表格数据
      craftList: [],
      // 产品查询
      productList:[],
      // 工序查询
      processList:[],
      // 物料查看
      materialselectoption:[],
      //设备查看
      equipmentselectoption:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      detailVisible:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        craftName: null,
        craftCode: null,
        productName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        craftName: [
          { required: true, message: "工艺名称不能为空", trigger: "blur" }
        ],
        craftCode: [
          { required: true, message: "工艺编码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 添加物料
    mtradd(index) {
      if (index == undefined) {
        this.nowmtrlist.push({
          materialName: null,
          materialNum: null,
          provider: null,
          materialCode: null,
        });
      } else {
        this.nowmtrlist.splice(index + 1, 0, {
          materialName: null,
          materialType: null,
          materialNum: null,
          provider: null,
          materialCode: null,
        });
      }

    },
    //添加工序
    processadd(index) {
       if(index == undefined) {
        this.form.processes.push({
          materials:[],
          processName:null,
          processCode:null,
          provider: null,
          station:null,
          equName:null
        });
       } else {
        this.form.processes.splice(index + 1,0,{
          materials:[],
          processName:null,
          processCode:null,
          provider: null,
          station:null,
          equName:null
        });
       }
    },
    /** 查询工艺管理列表 */
    getList() {
      this.loading = true;
      listCraft(this.queryParams).then(response => {
        this.craftList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      //查看下拉框的产品
      listProduct(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      // //查看所有工序
      // listProcess(this.queryParams).then(response => {
      //   this.processList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productName: null,
        productCode: null,
        id: null,
        craftName: null,
        craftCode: null,
        productId: null,
        productName: null,
        status: null,
        userId: null,
        createTime: null,
        note: null,
        processes:[],
        // material:[]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工艺管理";

        //查看所有工序
        listProcess(this.queryParams).then((response) => {
        console.log(response.rows);
        this.processList = response.rows;
      });
        //查看物料
      lookmaterial(this.queryParams).then((response) => {
        console.log(response.rows);
        this.materialselectoption = response.rows;
      });
        //查看设备
        listequipment(this.queryParams).then((response) => {
        console.log(response.rows);
        this.equipmentselectoption = response.rows;
      });

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCraft(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工艺管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(valid);
          if (this.form.id != null) {
            updateCraft(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCraftprocess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
      // 提交物料
      submitmat() {
        console.log(this.form.material)
          // addprocessmat(this.form.material).then(response => {
          //   console.log(response);
          //     // this.$modal.msgSuccess("新增成功");
          //     // this.open = false;
          //     // this.getList();
          //   });
      },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工艺管理编号为"' + ids + '"的数据项？').then(function() {
        return delCraft(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/craft/export', {
        ...this.queryParams
      }, `craft_${new Date().getTime()}.xlsx`)
    },
    //删除工序一行操作
    deleteRow(index, rows) {
      console.log('删除');
        rows.splice(index, 1);
      },
      // 删除物料一行
      deletemat(index, rows) {
      console.log('删除');
        rows.splice(index, 1);
      },
    //增加工序一行
      add() {
        var newLine = {
          processNum: this.processList.length + 1,
          
        }
      },
    // 下拉框选择产品对应编码
      bomchange(event) {
      this.form.productCode = this.productList.find(
        (item) => item.productName == event
      ).productCode;
      // console.log(this.form.productCode);
    },

    // 查看工序里的物料信息
    handlelook(index,row) {

      this.nowmtrlist=this.form.processes[index].materials
    
      this.detailVisible = true;
    }
  }
};
</script>

<style scoped>
@import url(../../../css/bootstrap.css);
</style>
